import { ElMessage } from 'element-plus'
const sign = {
  isConnectWS: false,
  // 主通道，用于开始签名、结束签名等
  websocket: null,
  wsUrl: 'ws://127.0.0.1:29999/',
  //获取操作系统类型
  getOSParams: {
    HWPenSign: 'HWGetOS',
  },
  //获取手写设备的连接状态
  deviceStatusParams: {
    HWPenSign: 'HWGetDeviceStatus',
  },
  // 打开设备,开始签名
  startSignParams: {
    HWPenSign: 'HWInitialize',
    key: 'E1FEE3A33612FB4AA712C0AF88A27CA3',
    title: '请在签字板上签字',
    nOrgX: 300,
    nOrgY: 300,
    nWidth: 500,
    nHeight: 300,
    nButtonHeight: 60,
    nImageWidth: -1,
    nImageHeight: -1,
    nFingerCap: 0,
    nConfirmTimeout: 30,
  },
  //关闭设备,签名结束
  endSignParams: {
    HWPenSign: 'HWFinalize',
  },
  connectCallback: null, //链接回调
  getOSCallback: null, //获取操作系统类型
  getDeviceStatusCallback: null, //获取设备状态
  startSignCallback: null, //打开设备回调
  endSignCallback: null,
  getDeviceIdCallback: null,
  signConfirmCallback: null, //确认签字，关闭设备
  reSetSignParam: () => {
    sign.startSignParams.nOrgX = -1
    sign.startSignParams.nOrgY = -1
    sign.startSignParams.nWidth = 500
    sign.startSignParams.nHeight = 300
    sign.startSignParams.nImageWidth = -1
    sign.startSignParams.nImageHeight = -1
    sign.startSignParams.nConfirmTimeout = 30
    sign.startSignParams.nFingerCap = 0
    sign.startSignParams.nButtonHeight = 60
  },
  logMessage: (message) => {
    if (typeof window.onHandleMessage !== 'undefined') window.onHandleMessage(message)
    else console.log(message)
  },
  // 连接websocket
  connectWebSocket: (callback) => {
    sign.connectCallback = callback
    if ('WebSocket' in window) {
      sign.websocket = new WebSocket(sign.wsUrl)
    } else if ('MozWebSocket' in window) {
      sign.websocket = new MozWebSocket(sign.wsUrl)
    } else {
      return
    }

    sign.websocket.binaryType = 'arraybuffer'
    sign.websocket.onopen = () => {
      console.log('链接签字服务成功，URL: ', sign.wsUrl)
      if (sign.websocket.readyState === 1) {
        sign.connectCallback(true)
      }
    }
    sign.websocket.onmessage = (evt) => {
      sign.wsMessage(evt)
    }
    sign.websocket.onclose = (evt) => {
      if (sign.websocket.readyState === 3) {
        console.log('链接关闭', evt)
        sign.connectCallback(false)
      }
    }
    sign.websocket.onerror = (evt) => {
      if (sign.websocket.readyState === 3) {
        console.log('链接报错', evt)
        sign.connectCallback(false)
        ElMessage.error('汉王签字板状态异常，请检查设备是否正常连接！')
      }
    }
  },
  /* 获取设备id */
  getDeviceId(callback) {
    sign.getDeviceIdCallback = callback
    if ('WebSocket' in window) {
      sign.websocket = new WebSocket(sign.wsUrl)
    } else if ('MozWebSocket' in window) {
      sign.websocket = new MozWebSocket(sign.wsUrl)
    } else {
      return
    }

    sign.websocket.binaryType = 'arraybuffer'
    sign.websocket.onopen = () => {
      if (sign.websocket.readyState === 1) {
        sign.sendMsg({ HWPenSign: 'HWGetDeviceId' })
      } else {
        sign.getDeviceIdCallback({})
      }
    }
    sign.websocket.onmessage = (evt) => {
      sign.wsMessage(evt)
    }
    sign.websocket.onclose = (evt) => {
      if (sign.websocket.readyState === 3) {
        console.log('链接关闭', evt)
        sign.getDeviceIdCallback({})
      }
    }
    sign.websocket.onerror = (evt) => {
      if (sign.websocket.readyState === 3) {
        console.log('链接报错', evt)
        sign.getDeviceIdCallback({})
      }
    }
  },
  // 向socket通道发送信息
  sendMsg: (param) => {
    if (sign.websocket && param) {
      sign.websocket.send(JSON.stringify(param))
    }
  },
  // websocket通道返回的信息
  wsMessage: (response) => {
    let res = JSON.parse(response.data)
    let cmd = res.HWPenSign
    switch (cmd) {
      case 'HWGetOS':
        sign.getOSCallback(res)
        break
      case 'HWGetDeviceStatus':
        sign.getDeviceStatusCallback(res)
        break
      case 'HWInitialize':
        sign.startSignCallback(res)
        break
      case 'HWGetSign':
        if (sign.signConfirmCallback !== null) sign.signConfirmCallback(res)
        break
      case 'HWFinalize':
        sign.endSignCallback(res)
        break
      case 'HWGetDeviceId':
        sign.getDeviceIdCallback(res)
        break
    }
  },
  // 断开检测服务器连接
  disconnectWebSocket: () => {
    if (sign.websocket) {
      if (sign.websocket.readyState === 1) sign.websocket.close()
      sign.websocket = null
      return true
    } else return false
  },
  // 服务连接出错
  onSocketError: () => {
    sign.logMessage('连接检测服务有问题...')
  },
  //获取操作系统类型
  getOS: (callback) => {
    sign.getOSCallback = callback
    sign.sendMsg(sign.getOSParams)
  },
  // 获取设备状态
  getDeviceStatus: (callback) => {
    sign.getDeviceStatusCallback = callback
    sign.sendMsg(sign.deviceStatusParams)
  },
  // 打开设备
  startSign: (callback, orgX, orgY, width, height, imgwidth, imgheight, fgrCap, timeout) => {
    sign.reSetSignParam()
    sign.startSignCallback = callback
    if (orgX > 0) sign.startSignParams.nOrgX = orgX
    if (orgY > 0) sign.startSignParams.nOrgY = orgY
    if (width > 0) sign.startSignParams.nWidth = width
    if (height > 0) sign.startSignParams.nHeight = height
    if (imgwidth > 0) sign.startSignParams.nImageWidth = imgwidth
    if (imgheight > 0) sign.startSignParams.nImageHeight = imgheight
    if (timeout > 0) sign.startSignParams.nConfirmTimeout = timeout
    if (fgrCap >= 0) sign.startSignParams.nFingerCap = fgrCap
    sign.sendMsg(sign.startSignParams)
  },
  // 确认签名
  getSign: (callback) => {
    sign.signConfirmCallback = callback
  },
  endSign: (callback) => {
    sign.endSignCallback = callback
    sign.sendMsg(sign.endSignParams)
  },
}
export default sign
